package dao.Impl;


import dao.CreateFile;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFileImpl implements CreateFile {
    private String fileName;
    private String newFileName;
    private String value;
    private String suffix;

    public CreateFileImpl(String value,String suffix) {
        this.value=value;
        this.suffix = suffix;
    }
    public String getFileName() {
        return newFileName;
    }

    @Override
    public void setPythonFileName(String name) {
        this.newFileName=name+System.currentTimeMillis();
    }

    @Override
    public void setJavaFileName() {
        if(value.contains("class")==false){
            this.newFileName="Error "+System.currentTimeMillis();
            return;
        }
        String []temp=null;
        temp=value.split("class ");
        if(temp.length<=1){
            fileName="Error";
        }else {
            if(temp[1].split("\\{").length==1)
                fileName="Error";
            else
                this.fileName=temp[1].split("\\{")[0];

        }

        this.newFileName=fileName+System.currentTimeMillis();
        value=value.replace(fileName,newFileName+" ");
    }

    @Override
    public void  writeFile() {
        try {
            BufferedWriter out = new BufferedWriter(new FileWriter("G:/compile/"+newFileName+"."+suffix));
            out.write(value);
            out.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
